package com.bocloud.cmp.service.utils;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;

import com.alibaba.fastjson.JSONObject;
import com.bocloud.cmp.dao.CloudServerDao;
import com.bocloud.cmp.dao.ClusterDao;
import com.bocloud.cmp.dao.DataStoreDao;
import com.bocloud.cmp.dao.HostSystemDao;
import com.bocloud.cmp.dao.NetworkCardDao;
import com.bocloud.cmp.dao.NetworkDao;
import com.bocloud.cmp.dao.NetworkHostDao;
import com.bocloud.cmp.dao.PortGroupDao;
import com.bocloud.cmp.dao.ServerDao;
import com.bocloud.cmp.dao.SnapshotDao;
import com.bocloud.cmp.dao.StoreHostDao;
import com.bocloud.cmp.dao.VdcDao;
import com.bocloud.cmp.entity.resource.CloudServer;
import com.bocloud.cmp.entity.resource.Cluster;
import com.bocloud.cmp.entity.resource.DataStore;
import com.bocloud.cmp.entity.resource.HostSystem;
import com.bocloud.cmp.entity.resource.Network;
import com.bocloud.cmp.entity.resource.PortGroup;
import com.bocloud.cmp.entity.resource.Server;
import com.bocloud.cmp.entity.resource.Vdc;
import com.bocloud.cmp.enums.VMStatus;
import com.bocloud.common.model.BsmResult;
import com.bocloud.common.utils.ListTool;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

/**
 * 同步VMware工具类
 * 
 * @author weiwei
 *
 */
public class SyncVmwareTool {
	private static Logger logger = LoggerFactory.getLogger(SyncVmwareTool.class);
	private VdcDao vdcDao;
	private CloudServerDao cloudServerDao;
	private DataStoreDao dataStoreDao;
	private HostSystemDao hostSystemDao;
	private SnapshotDao snapshotDao;
	private NetworkDao networkDao;
	private ClusterDao clusterDao;
	private StoreHostDao storeHostDao;
	private ServerDao serverDao;
	private NetworkCardDao networkCardDao;
	private PortGroupDao portGroupDao;
	private NetworkHostDao networkHostDao;
	private SyncVmwareUtils utils;

	public SyncVmwareTool() {
		super();
	}

	public SyncVmwareTool(VdcDao vdcDao, CloudServerDao cloudServerDao, DataStoreDao dataStoreDao,
			HostSystemDao hostSystemDao, SnapshotDao snapshotDao, NetworkDao networkDao, ClusterDao clusterDao,
			StoreHostDao storeHostDao, ServerDao serverDao, NetworkCardDao networkCardDao, PortGroupDao portGroupDao,
			NetworkHostDao networkHostDao) {
		super();
		this.vdcDao = vdcDao;
		this.cloudServerDao = cloudServerDao;
		this.dataStoreDao = dataStoreDao;
		this.hostSystemDao = hostSystemDao;
		this.snapshotDao = snapshotDao;
		this.networkDao = networkDao;
		this.clusterDao = clusterDao;
		this.storeHostDao = storeHostDao;
		this.serverDao = serverDao;
		this.networkCardDao = networkCardDao;
		this.portGroupDao = portGroupDao;
		this.networkHostDao = networkHostDao;
		this.utils = new SyncVmwareUtils(cloudServerDao, dataStoreDao, hostSystemDao, snapshotDao, networkDao,
				clusterDao, storeHostDao, networkCardDao, portGroupDao, networkHostDao);
	}

	/**
	 * 同步平台实体
	 * 
	 * @param vendor
	 * @param data
	 * @param userId
	 */
	public BsmResult sync(Long vendorId, Object data, Long userId) {
		try {
			if (null != data) {
				// 获取数据库的平台数据
				List<HostSystem> hosts = hostSystemDao.listByVendor(vendorId);
				Map<String, HostSystem> oldHostMap = Maps.newHashMap();
				for (HostSystem host : hosts) {
					oldHostMap.put(host.getUuid() + host.getHostIp(), host);
				}
				List<Vdc> vdcs = vdcDao.listByVendor(vendorId);
				Map<String, Vdc> oldVdcMap = Maps.newHashMap();
				for (Vdc vdc : vdcs) {
					oldVdcMap.put(vdc.getVdcVal(), vdc);
				}
				// 同步过来的对象集中存放
				List<HostSystem> allHost = Lists.newArrayList();
				// 获取同步数据
				String content = JSONObject.toJSONString(data);
				List<Vdc> allVdcs = JSONObject.parseArray(content, Vdc.class);
				for (Vdc vdc : allVdcs) {
					Long vdcId = null;
					if (oldVdcMap.containsKey(vdc.getVdcVal())) {
						Vdc oldVdc = oldVdcMap.get(vdc.getVdcVal());
						oldVdc.setMenderId(userId);
						oldVdc.setCpuTotal(vdc.getCpuTotal());
						oldVdc.setCpuUsed(vdc.getCpuUsed());
						oldVdc.setDiskTotal(vdc.getDiskTotal());
						oldVdc.setDiskUsed(vdc.getDiskUsed());
						oldVdc.setMemTotal(vdc.getMemTotal());
						oldVdc.setMemUsed(vdc.getMemUsed());
						oldVdc.setName(vdc.getName());
						vdcDao.update(oldVdc);
						vdcId = oldVdc.getId();
						// 移除存在更新的数据中心
						oldVdcMap.remove(vdc.getVdcVal());
					} else {
						vdc.preSave(userId, userId, vendorId);
						vdcDao.save(vdc);
						vdcId = vdc.getId();
					}
					// 同步网络
					List<Network> newNetworks = vdc.getNetworks();
					List<Network> networks = networkDao.listByVdc(vdcId);
					Map<String, Long> networkMap = Maps.newHashMap();
					Map<String, PortGroup> groupMap = Maps.newHashMap();
					syncNetwork(networks, newNetworks, vendorId, vdcId, userId, networkMap, groupMap);
					// 同步存储
					Map<String, Long> storeMap = new HashMap<>();
					List<DataStore> newStores = vdc.getStores();
					List<DataStore> stores = dataStoreDao.listByVdc(vdcId);
					syncStore(storeMap, stores, newStores, vendorId, vdcId, userId);
					// 同步宿主机
					List<HostSystem> newHostSystems = vdc.getHosts();
					if (null != newHostSystems) {
						allHost.addAll(newHostSystems);
						syncHost(storeMap, newHostSystems, oldHostMap, vendorId, vdcId, null, userId, networkMap,
								groupMap);
					}
					// 同步集群
					List<Cluster> newClusters = vdc.getClusters();
					List<Cluster> clusters = clusterDao.listByVdc(vdcId);
					syncCluster(clusters, newClusters, vendorId, vdcId, userId, allHost, storeMap, oldHostMap,
							networkMap, groupMap);
				}
				// 同步数据中心，删除不存在的数据中心
				syncVdc(oldVdcMap, userId);
				// 同步宿主机，删除不存在的宿主机
				syncHost(oldHostMap, userId);
				// 同步物理机和宿主机
				List<Server> listServer = serverDao.list(1, Integer.MAX_VALUE, null, null);
				List<HostSystem> listHost = hostSystemDao.listByVendor(vendorId);
				syncServer(listServer, listHost, userId);
			}
			return new BsmResult(true, "同步vmware平台数据成功");
		} catch (Exception e) {
			logger.error("Sync error:", e);
			return new BsmResult(false, "同步vmware平台数据失败");
		}

	}

	/**
	 * 同步数据中心(删除)
	 * 
	 * @param oldVdcs
	 * @param newVdcs
	 * @param userId
	 * @throws Exception
	 */
	private void syncVdc(Map<String, Vdc> oldVdcMap, Long userId) throws Exception {
		Iterator<Vdc> vdcs = oldVdcMap.values().iterator();
		while (vdcs.hasNext()) {
			vdcDao.remove(vdcs.next().getId(), userId);
		}
	}

	/**
	 * 同步集群(更新新增)
	 * 
	 * @param oldClusters
	 * @param newClusters
	 * @param vendorId
	 * @param vdcId
	 * @param userId
	 * @param allHost
	 * @param storeMap
	 * @param oldHostMap
	 * @throws Exception
	 */
	private void syncCluster(List<Cluster> oldClusters, List<Cluster> newClusters, Long vendorId, Long vdcId,
			Long userId, List<HostSystem> allHost, Map<String, Long> storeMap, Map<String, HostSystem> oldHostMap,
			Map<String, Long> networkMap, Map<String, PortGroup> groupMap) throws Exception {
		if (!ListTool.isEmpty(oldClusters) && ListTool.isEmpty(newClusters)) {
			clusterDao.removeByVdc(vdcId, userId);
		} else {
			Map<String, Cluster> oldMap = Maps.newHashMap();
			for (Cluster cluster : oldClusters) {
				oldMap.put(cluster.getClusterVal(), cluster);
			}
			for (Cluster cluster : newClusters) {
				Long clusterId = null;
				if (oldMap.containsKey(cluster.getClusterVal())) {
					Cluster oldCluster = oldMap.get(cluster.getClusterVal());
					// 更新集群
					utils.mergeCluster(oldCluster, cluster, userId);
					clusterId = oldCluster.getId();
					// 移除存在更新的集群
					oldMap.remove(cluster.getClusterVal());
				} else {
					cluster.preSave(userId, userId, vendorId, vdcId);
					clusterDao.save(cluster);
					clusterId = cluster.getId();
				}
				// 同步宿主机
				List<HostSystem> newHostSystems = cluster.getHosts();
				if (null != newHostSystems) {
					allHost.addAll(newHostSystems);
					syncHost(storeMap, newHostSystems, oldHostMap, vendorId, vdcId, clusterId, userId, networkMap,
							groupMap);
				}
			}

			// 删除不存在的集群
			Iterator<Cluster> clusters = oldMap.values().iterator();
			while (clusters.hasNext()) {
				clusterDao.remove(clusters.next().getId(), userId);
			}
		}
	}

	/**
	 * 同步网络
	 * 
	 * @param oldNetworks
	 * @param newNetworks
	 * @param vendorId
	 * @param vdcId
	 * @param userId
	 * @throws Exception
	 */
	private void syncNetwork(List<Network> oldNetworks, List<Network> newNetworks, Long vendorId, Long vdcId,
			Long userId, Map<String, Long> networkMap, Map<String, PortGroup> groupMap) throws Exception {
		if (ListTool.isEmpty(oldNetworks) && !ListTool.isEmpty(newNetworks)) {// 数据库不存在，该数据中心下存在网络，则全部新增
			// 新增
			for (Network network : newNetworks) {
				network.preSave(userId, userId, vendorId, vdcId);
				networkDao.save(network);
				networkMap.put(network.getNetworkVal(), network.getId());
				// 新增端口组
				List<PortGroup> groups = network.getGroups();
				if (!ListTool.isEmpty(groups)) {
					for (PortGroup group : groups) {
						group.preSave(userId, userId, vendorId, network.getId());
						portGroupDao.save(group);
						groupMap.put(group.getGroupVal(), group);
					}
				}
			}
		} else if (!ListTool.isEmpty(oldNetworks) && !ListTool.isEmpty(newNetworks)) {// 数据库存在，该数据中心下存在网络，则全部新增更新或删除
			Map<String, Network> oldMap = Maps.newHashMap();
			for (Network network : oldNetworks) {
				oldMap.put(network.getNetworkVal(), network);
			}
			// 不存在此网络则新增，否则更新
			for (Network network : newNetworks) {
				if (oldMap.containsKey(network.getNetworkVal())) {
					Network oldNetwork = oldMap.get(network.getNetworkVal());
					oldNetwork.setName(network.getName());
					oldNetwork.setStatus(network.getStatus());
					oldNetwork.setPortNum(network.getPortNum());
					oldNetwork.setMenderId(userId);
					networkDao.update(oldNetwork);
					networkMap.put(oldNetwork.getNetworkVal(), oldNetwork.getId());
					// 同步该网络下的端口组
					List<PortGroup> newGroups = network.getGroups();
					List<PortGroup> oldGroups = portGroupDao.listByNetwork(oldNetwork.getId());
					Map<String, PortGroup> oldGroup = Maps.newHashMap();
					for (PortGroup group : oldGroups) {
						oldGroup.put(group.getGroupVal(), group);
					}
					if (!ListTool.isEmpty(newGroups)) {
						for (PortGroup group : newGroups) {
							if (oldGroup.containsKey(group.getGroupVal())) {
								PortGroup portGroup = oldGroup.get(group.getGroupVal());
								portGroup.setName(group.getName());
								portGroup.setPortNum(group.getPortNum());
								portGroup.setRemark(group.getRemark());
								portGroup.setMenderId(userId);
								portGroupDao.update(portGroup);
								oldGroup.remove(group.getGroupVal());
								groupMap.put(portGroup.getGroupVal(), portGroup);
							} else {
								group.preSave(userId, userId, vendorId, oldNetwork.getId());
								portGroupDao.save(group);
								groupMap.put(group.getGroupVal(), group);
							}
						}
					}
					// 删除不存在的端口组
					Iterator<PortGroup> groups = oldGroup.values().iterator();
					while (groups.hasNext()) {
						portGroupDao.remove(groups.next().getId(), userId);
					}
					// 移除存在更新的网络
					oldMap.remove(network.getNetworkVal());
				} else {
					network.preSave(userId, userId, vendorId, vdcId);
					networkDao.save(network);
					networkMap.put(network.getNetworkVal(), network.getId());
					// 新增端口组
					List<PortGroup> groups = network.getGroups();
					if (!ListTool.isEmpty(groups)) {
						for (PortGroup group : groups) {
							group.preSave(userId, userId, vendorId, network.getId());
							portGroupDao.save(group);
							groupMap.put(group.getGroupVal(), group);
						}
					}
				}
			}
			// 删除不存在的网络
			Iterator<Network> networks = oldMap.values().iterator();
			while (networks.hasNext()) {
				Long networkId = networks.next().getId();
				portGroupDao.removeByNetwork(networkId, userId);
				networkDao.remove(networkId, userId);
			}
		} else if (!ListTool.isEmpty(oldNetworks) && ListTool.isEmpty(newNetworks)) {// 数据库存在，该数据中心不存在网络，则全部删除
			for (Network network : oldNetworks) {
				portGroupDao.removeByNetwork(network.getId(), userId);
			}
			networkDao.removeByVdc(vdcId, userId);
		}
	}

	/**
	 * 物理主机和该平台下的宿主机进行同步对比
	 * 
	 * @param servers
	 *            物理主机
	 * @param hosts
	 *            宿主机
	 * @throws Exception
	 */
	private void syncServer(List<Server> servers, List<HostSystem> hosts, Long userId) throws Exception {
		Map<String, Server> serverMap = Maps.newHashMap();
		for (Server server : servers) {
			serverMap.put(server.getUuid() + server.getManagerIp(), server);
		}
		// 数据库不存在该uuid，则添加物理主机
		for (HostSystem host : hosts) {
			Server server = null;
			if (serverMap.containsKey(host.getUuid() + host.getHostIp())) {
				server = serverMap.get(host.getUuid() + host.getHostIp());
				String status = host.getStatus();
				if (!status.equals(server.getStatus())) {
					server.setStatus(status);
					server.setMenderId(userId);
					serverDao.update(server);
				}
			} else {
				server = new Server();
				server.setName(host.getName());
				server.setUuid(host.getUuid());
				server.setManagerIp(host.getHostIp());
				server.setDiskCapacity(Integer.valueOf(host.getDiskTotal().toString()));
				server.setCpuHz(host.getCpuTotal().toString());
				server.setCreaterId(userId);
				server.setMenderId(userId);
				server.setStatus(host.getStatus());
				serverDao.save(server);
			}

		}
	}

	/**
	 * 同步数据存储
	 * 
	 * @param storeMap
	 * @param dataStores
	 * @param stores
	 * @param vendorId
	 * @param vdcId
	 * @param userId
	 * @return
	 * @throws Exception
	 */
	private void syncStore(Map<String, Long> storeMap, List<DataStore> oldStores, List<DataStore> newStores,
			Long vendorId, Long vdcId, Long userId) throws Exception {
		if (ListTool.isEmpty(oldStores) && !ListTool.isEmpty(newStores)) {// 数据库不存在，该虚拟机下存在快照，则全部新增
			// 新增
			for (DataStore dataStore : newStores) {
				dataStore.preSave(userId, userId, vendorId, vdcId);
				dataStoreDao.save(dataStore);
				storeMap.put(dataStore.getStoreVal(), dataStore.getId());
			}
		} else if (!ListTool.isEmpty(oldStores) && !ListTool.isEmpty(newStores)) {// 数据库存在，该虚拟机下存在快照，则全部新增更新或删除
			Map<String, DataStore> oldStore = Maps.newHashMap();
			for (DataStore store : oldStores) {
				oldStore.put(store.getStoreVal(), store);
			}
			// 同步数据存储更新新增
			for (DataStore dataStore : newStores) {
				if (oldStore.containsKey(dataStore.getStoreVal())) {
					DataStore store = oldStore.get(dataStore.getStoreVal());
					store.setName(dataStore.getName());
					store.setCapacity(dataStore.getCapacity());
					store.setFreeSpace(dataStore.getFreeSpace());
					store.setVdcId(vdcId);
					store.setMenderId(userId);
					store.setHostNum(dataStore.getHostNum());
					store.setVmNum(dataStore.getVmNum());
					store.setTemplateNum(dataStore.getTemplateNum());
					store.setStatus(dataStore.getStatus());
					dataStoreDao.update(store);
					storeMap.put(store.getStoreVal(), store.getId());
					// 移除存在更新的数据存储
					oldStore.remove(dataStore.getStoreVal());
				} else {
					dataStore.preSave(userId, userId, vendorId, vdcId);
					dataStoreDao.save(dataStore);
					storeMap.put(dataStore.getStoreVal(), dataStore.getId());
				}
			}
			// 同步数据存储删除不存在于平台上的数据
			Iterator<DataStore> stores = oldStore.values().iterator();
			while (stores.hasNext()) {
				dataStoreDao.remove(stores.next().getId(), userId);
			}
		} else if (!ListTool.isEmpty(oldStores) && ListTool.isEmpty(newStores)) {// 数据库存在，该虚拟机不存在快照，则全部删除
			dataStoreDao.removeByVdc(vdcId, userId);
		}
	}

	/**
	 * 同步宿主机(新增更新)
	 * 
	 * @param storeMap
	 * @param hostSystems
	 * @param hosts
	 * @param vendorId
	 * @param vdcId
	 * @param clusterId
	 * @param userId
	 * @param allServer
	 * @throws Exception
	 */
	private void syncHost(Map<String, Long> storeMap, List<HostSystem> hostSystems, Map<String, HostSystem> oldHostMap,
			Long vendorId, Long vdcId, Long clusterId, Long userId, Map<String, Long> networkMap,
			Map<String, PortGroup> groupMap) throws Exception {
		for (HostSystem hostSystem : hostSystems) {
			// 更新宿主机以及相关实体对象
			if (oldHostMap.containsKey(hostSystem.getUuid() + hostSystem.getHostIp())) {
				HostSystem host = oldHostMap.get(hostSystem.getUuid() + hostSystem.getHostIp());
				// 更新宿主机
				utils.mergeHost(host, hostSystem, vendorId, vdcId, clusterId, userId, storeMap, networkMap, groupMap);
				// 同步该宿主机下的虚拟机
				List<CloudServer> cloudServers = hostSystem.getServers();
				List<CloudServer> servers = cloudServerDao.listByHid(host.getId());
				syncServer(storeMap, servers, cloudServers, vendorId, host.getId(), userId, host.getPowerState(),
						networkMap, groupMap);
				// 移除存在更新的宿主机
				oldHostMap.remove(hostSystem.getUuid() + hostSystem.getHostIp());
			} else {
				hostSystem.preSave(userId, userId, vendorId, vdcId, clusterId);
				hostSystemDao.save(hostSystem);
				Long hostId = hostSystem.getId();
				// 保存物理网卡
				utils.addCard(vendorId, hostId, userId, "PM", hostSystem.getNetcards(), groupMap);
				// 保存虚拟机
				List<CloudServer> cloudServers = hostSystem.getServers();
				if (!ListTool.isEmpty(cloudServers)) {
					for (CloudServer server : cloudServers) {
						utils.addServerCascade(storeMap, server, vendorId, hostId, userId, hostSystem.getPowerState(),
								networkMap, groupMap);
					}
				}
				// 宿主机和存储建立关系
				utils.addStore(vendorId, hostId, userId, "PM", hostSystem.getStores(), storeMap);
				// 宿主机和网络建立关系
				utils.addNetwork(vendorId, hostId, userId, "PM", hostSystem.getNetworks(), networkMap, groupMap);
			}
		}
	}

	/**
	 * 同步宿主机(删除)
	 * 
	 * @param hostSystems
	 * @param hosts
	 * @param userId
	 * @throws Exception
	 */
	private void syncHost(Map<String, HostSystem> oldHostMap, Long userId) throws Exception {
		Iterator<HostSystem> hosts = oldHostMap.values().iterator();
		while (hosts.hasNext()) {
			Long hostId = hosts.next().getId();
			storeHostDao.deleteByHost(hostId, "PM");
			networkHostDao.deleteByHost(hostId, "PM");
			networkCardDao.deleteByHost(hostId, "PM", userId);
			List<CloudServer> servers = cloudServerDao.listByHid(hostId);
			for (CloudServer server : servers) {
				snapshotDao.removeByVm(server.getId(), userId);
				storeHostDao.deleteByHost(server.getId(), "VM");
				networkHostDao.deleteByHost(server.getId(), "VM");
				networkCardDao.deleteByHost(server.getId(), "VM", userId);
			}
			cloudServerDao.removeByHost(hostId, userId);
			hostSystemDao.remove(hostId, userId);
		}
	}

	/**
	 * 同步虚拟机
	 * 
	 * @param storeMap
	 * @param oldServers
	 * @param newServers
	 * @param vendorId
	 * @param hostId
	 * @param userId
	 * @throws Exception
	 */
	private void syncServer(Map<String, Long> storeMap, List<CloudServer> oldServers, List<CloudServer> newServers,
			Long vendorId, Long hostId, Long userId, String status, Map<String, Long> networkMap,
			Map<String, PortGroup> groupMap) throws Exception {
		if (ListTool.isEmpty(oldServers) && !ListTool.isEmpty(newServers)) {// 数据库不存在，该宿主机下存在虚拟机，则全部新增
			for (CloudServer cloudServer : newServers) {
				utils.addServerCascade(storeMap, cloudServer, vendorId, hostId, userId, status, networkMap, groupMap);
			}
		} else if (!ListTool.isEmpty(oldServers) && !ListTool.isEmpty(newServers)) {// 数据库存在，该宿主机下存在虚拟机，则新增、更新或删除
			Map<String, CloudServer> oldServer = Maps.newHashMap();
			for (CloudServer server : oldServers) {
				if (StringUtils.isEmpty(server.getInstanceId())) {
					oldServer.put(server.getName(), server);
				} else {
					oldServer.put(server.getInstanceId(), server);
				}
			}
			for (CloudServer cloudServer : newServers) {
				if (oldServer.containsKey(cloudServer.getInstanceId())
						|| oldServer.containsKey(cloudServer.getName())) {
					CloudServer server = null;
					if (oldServer.containsKey(cloudServer.getInstanceId())) {
						server = oldServer.get(cloudServer.getInstanceId());
						// 移除存在更新的虚拟机
						oldServer.remove(cloudServer.getInstanceId());
					} else {
						server = oldServer.get(cloudServer.getName());
						// 移除存在更新的虚拟机
						oldServer.remove(cloudServer.getName());
					}
					// 更新虚拟机
					utils.mergeServer(server, cloudServer, status, vendorId, hostId, userId, storeMap, networkMap,
							groupMap);
				} else {
					utils.addServerCascade(storeMap, cloudServer, vendorId, hostId, userId, status, networkMap,
							groupMap);
				}
			}
			// 删除不存在虚拟机及快照
			Iterator<CloudServer> servers = oldServer.values().iterator();
			while (servers.hasNext()) {
				CloudServer server = servers.next();
				if (VMStatus.BUILDING.name().equals(server.getStatus())) {
					continue;
				}
				snapshotDao.removeByVm(server.getId(), userId);
				storeHostDao.deleteByHost(server.getId(), "VM");
				networkHostDao.deleteByHost(server.getId(), "VM");
				networkCardDao.deleteByHost(server.getId(), "VM", userId);
				cloudServerDao.remove(server.getId(), userId);
			}
		} else if (!ListTool.isEmpty(oldServers) && ListTool.isEmpty(newServers))

		{// 数据库存在，该宿主机下不存在虚拟机，则全部删除（包括虚拟机以及快照）
			for (CloudServer server : oldServers) {
				snapshotDao.removeByVm(server.getId(), userId);
				storeHostDao.deleteByHost(server.getId(), "VM");
				networkHostDao.deleteByHost(server.getId(), "VM");
				networkCardDao.deleteByHost(server.getId(), "VM", userId);
			}
			cloudServerDao.removeByHost(hostId, userId);
		}
	}

}
